﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ingenious.Core
{
    public static class SerializerHelper
    {
        #region Constants
        private const int ErrorMessageMaximumLineLength = 100;
        private const string Ellipsis = "...";
        #endregion

        public static string FormatLineError(string errorLine)
        {
            if (errorLine.Length > ErrorMessageMaximumLineLength)
            {
                errorLine = errorLine.Substring(0, ErrorMessageMaximumLineLength) + Ellipsis;
            }

            return errorLine;
        }

        public static int IndexOf(this string source, int startIndex, Func<char, bool> predicate)
        {
            if (startIndex < source.Length)
            {
                string indexString = source.Substring(startIndex);
                int index = IndexOf<char>(indexString, predicate);
                
                if( index != -1 ) return index + startIndex;
            }

            return -1;
        }

        public static int IndexOf<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
        {
            int i = 0;

            foreach (TSource element in source)
            {
                if (predicate(element))
                    return i;

                i++;
            }

            return -1;
        }
    }
}
